package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LeadSingerTable extends Table {

	private static final String LEAD_SINGER_INSERT = "INSERT INTO LeadSinger VALUES (%s, '%s')";
	private static final String LEAD_SINGER_DELETE = "DELETE FROM LeadSinger WHERE upc=%s AND name='%s'";
	private static final String LEAD_SINGER_DISPLAY = "SELECT * FROM LeadSinger";
	
	private static final String LEAD_SINGER_UPC_QUERY = "SELECT * FROM LeadSinger WHERE name='%s'";
	
	private static final String LEAD_SINGER_EXISTS_QUERY = "SELECT * FROM LeadSinger where upc=%s and name='%s'";
	
	public LeadSingerTable()
	{
		super(LEAD_SINGER_INSERT, LEAD_SINGER_DELETE, LEAD_SINGER_DISPLAY, LEAD_SINGER_EXISTS_QUERY);
	}
	
	public enum Column
	{
		UPC(1, true),
		NAME(2, true);
		
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = {
				"upc",
				"name"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return index;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}

		public boolean isPrimaryKey() {
			return this.isPrimaryKey;
		}
	};
	
	public List<String> findUpcsForSinger(String singerName) throws SQLException
	{
		List<String> upcs = new ArrayList<String>();
		String fmtdQuery = String.format(LEAD_SINGER_UPC_QUERY, singerName);
		
		ResultSet rs = dbCon.executeQuery(fmtdQuery);
		if(rs != null)
		{
			while(rs.next())
			{
				upcs.add(rs.getString(Column.UPC.getIndex()));
			}
			
			rs.close();
		}		
		
		return upcs;
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}				
		
		return primaryKeyValues;
	}
	
	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
	
}

